package kr.hyosang;

import java.net.MalformedURLException;

import kr.hyosang.HttpUtil.OnProgressListener;

public class AsyncHttp extends Thread
{
     public interface AsyncHttpListener extends OnProgressListener {
        public void onComplete(HttpUtil httpUtil);
        public void onError(HttpUtil httpUtil);
    }
    
    private HttpUtil httpUtil;
    private AsyncHttpListener listener = null;
    
    public AsyncHttp(String url) throws MalformedURLException {
        httpUtil = new HttpUtil();
        httpUtil.setURL(url);
    }
    
    public void setListener(AsyncHttpListener listener) {
        this.listener = listener;
    }
    
    public void execute() {
        Thread t = new Thread(this);
        t.start();
    }
    
    @Override
    public void run() {
        try {
            httpUtil.setListener(listener);
            httpUtil.process();
            
            if(listener != null) {
                listener.onComplete(httpUtil);
            }
        }catch(NullPointerException e) {
            e.printStackTrace();
            if(listener != null) {
                listener.onError(httpUtil);
            }
        }
    }

}
